/**  
* @description
* @author  wangshiwei  
* @date   2018年6月25日    
*/  
package com.oren.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**  
* @description
* @author  wangshiwei  
* @date   2018年6月25日 下午4:59:08
    
*/
public final class JsonUtil {
	
	private static final Logger LOGGER=LoggerFactory.getLogger(JsonUtil.class);

	private static final ObjectMapper OBJECT_MAPPER=new ObjectMapper();
	
	/**
	 * 将pojo转换为实体类
	 * @param obj
	 * @return
	 */
	public static <T> String pojoToJson(T obj) {
		String json;
		
		try {
			json=OBJECT_MAPPER.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			LOGGER.error("convert POJO to JSON failure",e);
			throw new RuntimeException(e);
		}
		return json;
	}
	
	public static <T> T  jsonToPojo(String json,Class<T> type) {
		T pojo;
		try {
			pojo=OBJECT_MAPPER.readValue(json, type);
		} catch (Exception e) {
			LOGGER.error("convert json to pojo failure",e);
			throw new RuntimeException(e);
		}
		return pojo;
	}
	
}
